package main

// 模拟
func uniquePathsWithObstacles(obstacleGrid [][]int) int {
	if obstacleGrid[0][0] == 1 || obstacleGrid[len(obstacleGrid)-1][len(obstacleGrid[0])-1] == 1 {
		return 0
	}

	step := make([]int, len(obstacleGrid[0]))
	step[0] = 1
	for i := 0; i < len(obstacleGrid); i++ {
		for r := 0; r < len(obstacleGrid[i]); r++ {
			if obstacleGrid[i][r] == 1 {
				// 障碍物
				continue
			}
			if i != 0 && obstacleGrid[i-1][r] == 1 {
				step[r] = 0
			}
			if r != 0 && obstacleGrid[i][r-1] != 1 {
				step[r] += step[r-1]
			}
		}
	}
	return step[len(step)-1]
}

func main() {
	//p := [][]int{{0, 0, 0}, {0, 1, 0}, {0, 0, 0}}
	p := [][]int{{0, 0}, {1, 1}, {0, 0}}
	res := uniquePathsWithObstacles(p)
	println(res)
}
